package com.zjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.annotation.domain.ResponseResult;
import com.zjw.annotation.domain.entity.Tag;
import com.zjw.annotation.domain.vo.PageVo;
import com.zjw.annotation.domain.vo.TagVo;
import com.zjw.enums.AppHttpCodeEnum;
import com.zjw.mapper.TagMapper;
import com.zjw.service.TagService;
import com.zjw.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author lkzy
 * @since 2023-12-19 15:00:04
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {


    @Override
    public ResponseResult getTagList(Long pageNum, Long pageSize) {
        IPage<Tag> page = new Page(pageNum, pageSize);
        page(page, null);

        PageVo res = new PageVo();
        res.setRows(BeanCopyUtils.copyList(page.getRecords(), TagVo.class));
        res.setTotal(page.getTotal());
        return ResponseResult.okResult(res);
    }

    private Boolean tagNameExist(String name, Long id) {
        TagMapper mapper = getBaseMapper();
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        wrapper.ne("id", id);
        if (!list(wrapper).isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public ResponseResult addTag(Tag tag) {
        if (tagNameExist(tag.getName(), tag.getId()))
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_INVALID_CHECK);
        save(tag);
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult getTag(Long id) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return ResponseResult.okResult(BeanCopyUtils.copy(getOne(wrapper), TagVo.class));
    }

    @Override
    public ResponseResult alterTag(Tag tag) {
        if (tagNameExist(tag.getName(), tag.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_INVALID_CHECK);
        }
        updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delTag(List<Long> ids) {
        for (Long id : ids) {
            removeById(id);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllTag() {
        List<Tag> list = list(null);
        List<TagVo> res = BeanCopyUtils.copyList(list, TagVo.class);
        return ResponseResult.okResult(res);
    }

    public List<Tag> getTagsByArticleId(Long id){
        TagMapper mapper = getBaseMapper();
        List<Tag> res = mapper.getTagsByArticleId(id);
        return res;
    }

    @Override
    public void addArticleTag(Long id, long tId) {
        TagMapper mapper = getBaseMapper();
        mapper.addArticleTag(id, tId);
    }

    @Override
    public void deleteTags(Long id) {
//        删除指定文章的所有标签
        TagMapper mapper = getBaseMapper();
        mapper.restTags(id);
    }
}

